In [1]:
#instalé fiona porque me salía un error, sin embargo este error sigue sin solucionarse.
#Opté por dejarlo ahí 
!pip install fiona
Requirement already satisfied: fiona in c:\users\intel\anaconda3\lib\site-packages (1.8.21)
Requirement already satisfied: attrs>=17 in c:\users\intel\anaconda3\lib\site-packages (from fiona) (21.4.0)
Requirement already satisfied: certifi in c:\users\intel\anaconda3\lib\site-packages (from fiona) (2022.9.14)
Requirement already satisfied: click>=4.0 in c:\users\intel\anaconda3\lib\site-packages (from fiona) (8.0.4)
Requirement already satisfied: cligj>=0.5 in c:\users\intel\anaconda3\lib\site-packages (from fiona) (0.7.2)
Requirement already satisfied: click-plugins>=1.0 in c:\users\intel\anaconda3\lib\site-packages (from fiona) (1.1.1)
Requirement already satisfied: six>=1.7 in c:\users\intel\anaconda3\lib\site-packages (from fiona) (1.16.0)
Requirement already satisfied: munch in c:\users\intel\anaconda3\lib\site-packages (from fiona) (2.5.0)
Requirement already satisfied: setuptools in c:\users\intel\anaconda3\lib\site-packages (from fiona) (63.4.1)
Requirement already satisfied: colorama in c:\users\intel\anaconda3\lib\site-packages (from click>=4.0->fiona) (0.4.5)
In [2]:
import os, geopandas as gpd

municipios=gpd.read_file(os.path.join("maps","Municipios","municipio_geo.shp"))
ciudades=gpd.read_file(os.path.join("maps","Ciudades","CapitalDepartamental.shp"))
rios=gpd.read_file(os.path.join("maps","Rios","Rios.shp"))
In [3]:
#Qué tipo es
type(municipios)
Out[3]:
geopandas.geodataframe.GeoDataFrame
In [4]:
#Qué tipo es
type(rios)
Out[4]:
geopandas.geodataframe.GeoDataFrame
In [5]:
#Qué tipo es
type(ciudades)
Out[5]:
geopandas.geodataframe.GeoDataFrame
In [6]:
#Dimensión
municipios.shape
Out[6]:
(339, 7)
In [7]:
#Dimensión
rios.shape
Out[7]:
(940, 5)
In [8]:
#Dimensión
ciudades.shape
Out[8]:
(9, 4)
In [9]:
#Observamos un poco del contenido
municipios.head()
Out[9]:
id c_ut DEPARTAMEN PROVINCIA MUNICIPIO CAPITAL geometry
0 0 040505 Oruro Litoral Esmeralda Esmeralda POLYGON ((-68.26703 -18.81616, -68.26279 -18.8...
1 1 050203 Potosí Rafael Bustillo Llallagua Llallagua POLYGON ((-66.42997 -18.25647, -66.42895 -18.2...
2 2 050702 Potosí Alonso de Ibáñez Caripuyo Caripuyo POLYGON ((-66.55964 -18.07798, -66.55916 -18.0...
3 3 030201 Cochabamba Campero Aiquile Aiquile POLYGON ((-65.21781 -17.92884, -65.21745 -17.9...
4 4 050701 Potosí Alonso de Ibáñez Sacaca Villa de Sacaca POLYGON ((-66.31737 -17.95174, -66.31672 -17.9...
In [10]:
#Observamos un poco del tipo de contenido que tiene
municipios.info()
<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 339 entries, 0 to 338
Data columns (total 7 columns):
 #   Column      Non-Null Count  Dtype   
---  ------      --------------  -----   
 0   id          339 non-null    int32   
 1   c_ut        339 non-null    object  
 2   DEPARTAMEN  339 non-null    object  
 3   PROVINCIA   339 non-null    object  
 4   MUNICIPIO   339 non-null    object  
 5   CAPITAL     339 non-null    object  
 6   geometry    339 non-null    geometry
dtypes: geometry(1), int32(1), object(5)
memory usage: 17.3+ KB
In [11]:
#Observamos un poco del contenido
rios.head()
Out[11]:
gml_id ID NOMBRE RIOS_LIM geometry
0 Rios.1 0 Río Kharamanu None LINESTRING (-67.70626 -10.69996, -67.70825 -10...
1 Rios.2 0 Río Paucerna None LINESTRING (-60.81141 -14.27497, -60.80161 -14...
2 Rios.3 0 Río Supaymanu None LINESTRING (-68.56330 -11.46430, -68.56529 -11...
3 Rios.4 0 None None LINESTRING (-68.92574 -11.17870, -68.92760 -11...
4 Rios.5 0 None None LINESTRING (-66.50216 -10.69610, -66.50555 -10...
In [12]:
#Observamos un poco del tipo de contenido que tiene
rios.info()
<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 940 entries, 0 to 939
Data columns (total 5 columns):
 #   Column    Non-Null Count  Dtype   
---  ------    --------------  -----   
 0   gml_id    940 non-null    object  
 1   ID        940 non-null    int64   
 2   NOMBRE    189 non-null    object  
 3   RIOS_LIM  13 non-null     object  
 4   geometry  940 non-null    geometry
dtypes: geometry(1), int64(1), object(3)
memory usage: 36.8+ KB
In [13]:
#Observamos un poco del contenido
ciudades.head()
Out[13]:
gml_id ID NOMBRE geometry
0 CapitalDepartamental.1 0 COBIJA POINT (-68.77689 -11.03397)
1 CapitalDepartamental.2 0 TRINIDAD POINT (-64.92070 -14.84987)
2 CapitalDepartamental.3 0 LA PAZ POINT (-68.13798 -16.49165)
3 CapitalDepartamental.4 0 COCHABAMBA POINT (-66.17044 -17.38918)
4 CapitalDepartamental.5 0 ORURO POINT (-67.11293 -17.96623)
In [14]:
#Observamos un poco del tipo de contenido que tiene
ciudades.info()
<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 9 entries, 0 to 8
Data columns (total 4 columns):
 #   Column    Non-Null Count  Dtype   
---  ------    --------------  -----   
 0   gml_id    9 non-null      object  
 1   ID        9 non-null      int64   
 2   NOMBRE    9 non-null      object  
 3   geometry  9 non-null      geometry
dtypes: geometry(1), int64(1), object(2)
memory usage: 416.0+ bytes
In [15]:
#plot simple de todos
municipios.plot()
rios.plot()
ciudades.plot()
Out[15]:
<AxesSubplot:>
In [16]:
#plot cambiando algunas carcateristicas
municipios.plot(facecolor="palegreen",#color de relleno poligonal
               edgecolor='black', #color de lineas
               linewidth=0.4) #grosor de lineas
Out[16]:
<AxesSubplot:>
In [17]:
#plot cambiando algunas carcateristicas
rios.plot(edgecolor='blue', 
            linewidth=0.3,
            linestyle='solid')
Out[17]:
<AxesSubplot:>
In [18]:
#plot cambiando algunas carcateristicas
ciudades.plot(marker='D', #tipo de marca
            color='red', 
            markersize=5,
            alpha=1) #transparencia
Out[18]:
<AxesSubplot:>
In [19]:
municipios.crs
Out[19]:
<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
In [20]:
rios.crs
Out[20]:
<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
In [21]:
ciudades.crs
Out[21]:
<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
In [22]:
#todos tienen el mismo crs, por lo tanto podemos agruparlos
base = municipios.plot(facecolor="white", edgecolor='black', linewidth=0.25,figsize=(12,12))
ciudades.plot(marker='D', color='red', markersize=15,alpha=1,
            ax=base) 
rios.plot(edgecolor='blue', linewidth=0.35,
            ax=base)
Out[22]:
<AxesSubplot:>
In [23]:
import folium


m = ciudades.explore(color="red", 
                   name="ciudades")

m = rios.explore(m=m, color="blue",
                   name="rios")
m
Out[23]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [24]:
sucre=municipios[municipios.MUNICIPIO=='Sucre']
In [25]:
ciudades_clipped = gpd.clip(gdf=ciudades,
                          mask=sucre)
rios_clipped = gpd.clip(gdf=rios,
                               mask=sucre)
In [26]:
base = sucre.plot(facecolor="lightgreen", edgecolor='black', linewidth=0.6,figsize=(5,5))
ciudades_clipped.plot(marker='o', color='red', markersize=15,ax=base)
rios_clipped.plot(edgecolor='blue', linewidth=0.5,ax=base)
Out[26]:
<AxesSubplot:>
In [27]:
sucreCoord=[-19.03332, -65.26274]
In [28]:
m = ciudades.explore(location=sucreCoord,
                   zoom_start=4.5,
                   tiles='CartoDB positron',
                   color='red',
                   name="ciudades")
m = rios.explore(m=m, color="blue",
                   name="rios")

m
Out[28]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [29]:
#Guardamos el mapa como una imagen
import matplotlib.pyplot as plt

base = sucre.plot(facecolor="lightgreen", edgecolor='black', linewidth=0.6,figsize=(5,5))
map2=ciudades_clipped.plot(marker='o', color='red', markersize=15,ax=base)
mapEnd=rios_clipped.plot(edgecolor='blue', linewidth=0.5,ax=base)
plt.savefig(os.path.join("maps",'mapEnd.jpg'))
In [30]:
#Exportamos los mapas a un tipo de archivo diferente al de shapefile
municipios.to_file(os.path.join("maps","worldMaps.gpkg"), layer='municipios', driver="GPKG")
ciudades.to_file(os.path.join("maps","worldMaps.gpkg"), layer='ciudades', driver="GPKG")
rios.to_file(os.path.join("maps","worldMaps.gpkg"), layer='rios', driver="GPKG")
In [31]:
worldMaps='https://github.com/FabianRomero20/Examen1/raw/main/maps/worldMaps.gpkg'